// pages/exercises/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    course: null,
    submitObj: {},
    analysis: false,
    showImage: false,
    questionNum: 1,
    oindex: 0, //这个oindex 作为题目的类型的索引，单选，多选，判断等
    qindex: 0, //qindex 就是 questionindex 是题目真正的索引
    qoindex: 0, //questinOptionIndex 题目选项的索引
    maxSize: 0, // maxSize 题目的最大数
    qmaxSize: 0,
    exercisesTitle: [],
    exercisesItems: [],
    exercisesAsk: [],
    exercises: [],
    showButton: false,
    server: app.data.server,
    answerarr: ['A', 'B', 'C', 'D', 'E', '正确', '错误'],
    correctAnswer: "",
    qestionType: "",
    question: "",
    questionList: "",
    questionOptions: "",
    questionAnalysis: "",
    questionHistory:[],
    questionHistoryAnswer:"",
    qusiontHistroyOption:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    options.userId = app.globalData.userInfo.userCode;
    var _this = this;
    this.setData({
      course: options
    })
    
    
       
    var that = this;
    var data = {};
     
    app.paiRequest("/app/course/tcourses/exercisesNew", { parentCode: options.courseHourseId }).then(function (res) {
      var arry = res.data;
      if (typeof (res.data) === "string") {
        arry = JSON.parse(res.data)
      }

      var len = arry.maxSize;
      var temp_question = arry.data[_this.data.oindex].childList[_this.data.qindex];
       
      var temp_questions_list = arry.data[_this.data.oindex];
      

      app.paiRequest("/app/course/exercise/record/findListNew", options).then(function (res) {
        var qhistoryList = res.data;

        for (var i = 0; i < qhistoryList.length; i++) {
          var qid = qhistoryList[i].questionId;

          if (qid === temp_question.id) {
             
            _this.setData({
              questionHistoryAnswer: qhistoryList[i].flag,
              qusiontHistroyOption: qhistoryList[i].answerId
            })
          }
        }
      })
 
      

      _this.setData({
        qestionType: arry.data[_this.data.oindex],
        question: temp_question,
        questionList: temp_questions_list,
        maxSize: len,
        //层级结果
        //arry.data[index] 题的大类，单选或多选
        //arry.data[index].childList[index] 具体的题目信息，包含题的标题，选项等
        //arry.data[_this.data.oindex].childList[_this.data.qindex].childList 具体的题的选项集合
        questionOptions: arry.data[_this.data.oindex].childList[_this.data.qindex].childList,
        correctAnswer: temp_question.answerStr,
        questionAnalysis: temp_question.analysis,
        showImage:true
      })





    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },



  /**
   * 单选事件
   */
  radioChang: function (e) {
    var _this = this;
    var questionId = e.currentTarget.dataset.id;
    var answerId = e.detail.value;
    var course = this.data.course;
    var type = e.currentTarget.dataset.type;
    var options = this.data.course;
   



  },
  /**
   * 多选事件
   * 点击5秒后请求后台校验选项的正确性
   */
  checkboxChange: function (e) {
    var questionId = e.currentTarget.dataset.id;
    var answerId = e.detail.value;
    var submitObj = this.data.submitObj;
    submitObj[questionId] = answerId;
    var type = e.currentTarget.dataset.type;
    var options = this.data.course;
    var _this = this;
   


  },
  /**
   * 提交答案
   */
  submitQuestion: function (e) {
     
    
      wx.navigateBack({
        delta: 1
       
    })
  },
  nextList: function () {
    this.setData({
      analysis: true,
      showImage: true
    })
    var _this = this;
    //得到当前题型是多选题还是判断题的集合
    var questionList = this.data.questionList;
     
    //获取当前题的所以
    var questionIndex = this.data.qindex;
    //获取当前题的类型
    var quesiontType = this.data.question.courseItem;

    var questionLength = this.data.maxSize;
    //获取下一个题的索引
    let temp_questionIndex = questionIndex + 1;
    //获取下一个题的题号，用于显示
    let next_questionNum = _this.data.questionNum + 1;

    var nextQuestion;
    if (next_questionNum === questionLength) {

      nextQuestion = questionList.childList[questionLength-1];
      //最后一题，就没有下一题了，直接显示返回
      _this.setData({
        showButton: true,
        showImage: true
      })
    } else {
      nextQuestion = questionList.childList[temp_questionIndex];
    }
    //重初始化函数拿到初始化函数中的相关变量
    var options = _this.data.course;
    //得到回显的答案，因为没时间看怎么用函数，这里复制了下
    app.paiRequest("/app/course/exercise/record/findListNew", options).then(function (res) {
      var qhistoryList = res.data;

      for (var i = 0; i < qhistoryList.length; i++) {
        var qid = qhistoryList[i].questionId;
        
        if (qid === nextQuestion.id) {
          //多选题的选项需要特殊处理
          if (nextQuestion.courseItem=="1"){
            var multipleOptions = qhistoryList[i].answerId.split(",");
            //这里直接控制勾选状态，前端不吃复杂的表达式
            var nextQuestionOptions= nextQuestion.childList;
            for (var j = 0; j < nextQuestionOptions.length;j++){
            
              if (qhistoryList[i].answerId.indexOf(nextQuestionOptions[j].id)!=-1){
                
                  nextQuestionOptions[j].checked=true;
              }else{
                  nextQuestionOptions[j].checked = false;
              }
            }
            nextQuestion.childList = nextQuestionOptions;
            
            _this.setData({
              questionHistoryAnswer: qhistoryList[i].flag,
              qusiontHistroyOption: multipleOptions,
              questionOptions: nextQuestion.childList,
            })
          }else{
            _this.setData({
              questionHistoryAnswer: qhistoryList[i].flag,
              qusiontHistroyOption: qhistoryList[i].answerId
            })
          }
        
        }
      }
    })

    //如果下一个题是不用类型，索引重置
    if (nextQuestion.courseItem != quesiontType) {
      //初始化改类型题下的索引
      _this.setData({
        qindex: 0,
        questionNum: next_questionNum,
        question: nextQuestion,
        questionOptions: nextQuestion.childList,
        questionAnalysis: nextQuestion.analysis,
        //下一题不显示答案，以防上一题的答案混淆
        showImage: true,
        correctAnswer: nextQuestion.answerStr,
      })
    } else {
      _this.setData({
        qindex: temp_questionIndex,
        questionNum: next_questionNum,
        question: nextQuestion,
        questionOptions: nextQuestion.childList,
        questionAnalysis: nextQuestion.analysis,
        //下一题不显示答案，以防上一题的答案混淆
        showImage: true,
        correctAnswer: nextQuestion.answerStr,
      })
    }

    //保留回显，这段逻辑无用了
    var obje;
    if (obje == undefined) {

    } else {

      //这里是取数据为了做回显
      // app.paiRequest("/app/course/tcourses/exercisesOptions", {
      //   parentCode: questionList[questionIndex + 1].id
      // }).then(function (ress) {
      //   var options = ress.data.data;
      //   var type = ress.data.objType;
      //   var answer = ress.data.answer;
      //   var correctAnswer=[];
      //   var correctStr = "";
      //   //如果是多选
      //   if(type==1) {
      //     for (var i = 0; i < answer.length; i++) {
      //       if(answer[i].answer==1){
      //         var newVar = _this.data.answerarr[i];
      //       }

      //       correctAnswer.push(newVar);

      //     }
      //     correctStr = correctAnswer.join();
      //   }else {
      //     for (var i = 0; i < answer.length; i++) {
      //       if(answer[i].answer==1){
      //         var newVar = _this.data.answerarr[i];
      //         correctStr = newVar;
      //       }

      //     }
      //     //除了多选，这里就是单选，不管是语音还是案例

      //   }



      //   for (var i = 0; i < options.length; i++) {
      //     options[i].checked = false;
      //   }
      //   _this.setData({
      //     questionOptions: options,
      //     correctAnswer:correctStr
      //   })
      // })
    }
  }
})